package com.jet.rtdb.elasticsearch.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author: zhangkaifeng.
 * @CreateTime: 2017/8/16 11:25
 * @Description:获取客户端连接
 */


public class EsConfig {
    private static volatile TransportClient client = null;

    private static final String host = "192.168.1.110";

    private static final int port = 9300;

    public static TransportClient getInstance() {
        if (client == null) {
            synchronized (EsConfig.class) {
                if (client == null) {
                    Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("client.transport.sniff", true).build();
                    try {
                        client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return client;
    }
}
